// file operator pool test
// g++ -g -o fp pool_test.cpp -I/home/sun/workspace/SDFS2 -I/home/sun/libs/muduo/include -L/home/sun/libs/muduo/lib -lmuduo_base -lmuduo_net -lpthread -lboost_system -lboost_filesystem


#include <sdfs/common/Pool.h>
//#include "PoolData.h"
#include <muduo/base/Types.h>
#include <muduo/base/Logging.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <sdfs/common/File.h>
#include <vector>


using namespace std;
using namespace boost;
using namespace muduo;
using namespace sdfs::common;


int main()
{
	char pathbuf[256];
	muduo::string filepath = "/home/sun/sdfs_bin";
	shared_ptr< ReadOnlyFile > pd;
	Pool< ReadOnlyFile > pool;

	char msg[] = "hello";
	for (int i = 0; i < 3; ++i)
	{
		snprintf(pathbuf, 256, "%s/%d", filepath.c_str(), i+1);
		muduo::string p(pathbuf);
		WriteOnlyFile writer(p);
		for (int i = 0; i < 100; ++i)
		{
			writer.write(msg, sizeof(msg));
		}
	}
	for(int i = 0 ; i < 3; ++i)
	{
		snprintf(pathbuf, 256, "%s/%d", filepath.c_str(), i+1);
		muduo::string p(pathbuf);

		ReadOnlyFile* reader = new ReadOnlyFile(p);
		reader->registerToPool(&pool);
		//cout << "reader reference: " << reader.use_count() << endl;
	}
	for (int i = 0; i < 3; ++i)
	{
		shared_ptr<ReadOnlyFile> reader = pool.getData();
		LOG_DEBUG << reader.use_count();
		reader->seekFromBeginning(0);
		reader->read(msg, sizeof(msg));
		cout << "read: " << msg <<endl;
	}
	/*
	{
//		shared_ptr<PoolData<Data> > pdata = pool.getData();
		shared_ptr<ReadOnlyFile> d = pool.getData();
		cout<<"may be error"<<endl;
		cout<< "pdata: "<< d << endl;
	}
	{
		shared_ptr<PoolData<Data> > pdata = pool.getData();
		cout<< "pdata: "<< pdata->getData()->v() << endl;
		pdata = pool.getData();
		cout<< "pdata: "<< pdata->getData()->v() << endl;

	}*/
	cout<< "end process" << endl;
}
